<?php 
/**
 * ypAdminAction
 * Manager Action, class, path and method for admin
 * 
 * @package ypCore 1.0
 * @since ypCore 1.0
 * @author duyet2000@gmail.com (http://go.use.vn)
 * @copyright 2013
 * @version $Id$
 * @access public
 */
abstract class ypAdminController {
	protected $_registry;
	protected $_id;
	protected $_layout;
	protected $_template;
	protected $_templateDir;
	protected $_children = array();
	protected $_data = array();
	protected $_output;

	public function __construct(&$_registry) {
		$this->_registry = $_registry;

		$this->_template = $this->_registry->get('Setting')->get('admin_template');
		$this->_templateDir = ADMIN_TEMPLATE_DIR . '/' . $this->_template;

		// Default 
		$this->set('template_url', $this->Request->getSiteUrl() . '/' . ADMIN_DIR . '/view/' . $this->_template);
		$this->set('site_url', $this->Request->getSiteUrl());
		$this->set('static_url', $this->Request->getSiteUrl() . '/' . YPCORE_STATIC_DIR);
	}

	public function __get($key) {
		return $this->_registry->get($key);
	}

	public function __set($key, $value) {
		$this->_registry->set($key, $value);
	}

	public function set($key, $value) {
		$this->_data["$key"] = $value;
	}

	public function loadLanguage($group) {
		$group = (string) $group;
		$this->Language->load($group);
		if ($this->Language->phrase) {
			$this->_data = array_merge($this->_data, $this->Language->phrase);
		}
	}

	public function setTemplate($path) {
		$this->_template = $path;
	}

	public function setTemplateChild($child = array()) {
		$this->_children = array_merge($this->_children, $child);
	}

	protected function forward($route, $args = array()) {
		return new ypAction($route, $args);
	}

	protected function redirect($url, $status = 302) {
		header('Status: ' . $status);
		header('Location: ' . str_replace(array('&amp;', "\n", "\r"), array('&', '', ''), $url));
		exit();
	}

	protected function getChild($child, $args = array()) {
		$action = new ypAction($child, $args);
	
		if (file_exists($action->getFile())) {
			require_once($action->getFile());

			$class = $action->getClass();

			$controller = new $class($this->_registry);

			if($action->getArgs()) {
				call_user_func_array(array($controller, $action->getMethod()), $action->getArgs());
			} else {
				call_user_func(array($controller, $action->getMethod()));
			}

			return $controller->_output;
		} else {
			trigger_error('Error: Could not load controller ' . $child . '!');
			exit();
		}		
	}

	protected function render() {
		foreach ($this->_children as $child) {
			$this->_data[basename($child)] = $this->getChild($child);
		}

		// Auto title 
		if (isset($this->_data['title'])) {
			$this->Document->setTitle($this->_data['title']);
		}

		$templatePath = $this->_templateDir  . '/' . $this->_template;
		if (!file_exists($templatePath)) {
			trigger_error('Error: Could not load template ' . $templatePath . '!');
			exit();
		}

		if ($this->_registry->get('Setting')->get('compress_html')) {
			$templateCacheName = 'admin-' . $this->_templateName . '-' . str_replace('/', '-', $this->_template);
			$template_cache_file = __CACHETEMPLATE::getInstance()->get($templateCacheName);
			if (!$template_cache_file) {
				$template = file_get_contents($templatePath);
				$option['compressJs'] = false;
				$option['compressCss'] = true;
				$template = __COMPRESSHTML::getInstance()->compress($template, $option);
				if ($template) {
					__CACHETEMPLATE::getInstance()->set($templateCacheName, $template);
				}
				$template_cache_file = __CACHETEMPLATE::getInstance()->get($templateCacheName);
			}
			$templatePath = $template_cache_file;
		}

		extract($this->_data);

		ob_start();
		require ($templatePath);
		$this->_output = ob_get_contents();
		ob_end_clean();

		return $this->_output;
	}

	public function checkLogin() {
		if (!$this->User->isAdmin()) {
			$this->redirect($this->Link->build('Admin/Login/Index'));
		}

		$sessionAdmin = (string) $this->Session->get('admin');
		if ($sessionAdmin) {
			$this->Document->set('is_login', TRUE);
			return TRUE;
		}

		$cookieAdmin = $this->Cookie->get('admin');
		$cookieAdminCode = $this->Cookie->get('admin_code');
		if(!$cookieAdmin OR !$cookieAdminCode) {
			$this->redirectLogin();
		}
		
		$decode = $this->Crypt->userLoginInfoDecode($cookieAdminCode);
		if (!$decode OR $decode['username'] != $cookieAdmin) {
			$this->redirectLogin();
		}
		
		$this->document->set('is_login', TRUE);
		$this->_registry->get('Session')->set('admin', $cookieAdmin);
		return TRUE;
	}

	public function redirectLogin() {
		$this->Link->addParam('redirect_to', $this->Link->build($this->Link->this(), TRUE));
		$this->Link->addParam('admin_username', $this->User->get('username'));
		$this->Link->addParam('relogin', '1');
		$this->redirect($this->Link->build('Admin/Login/Index', TRUE, $this->Link->getParams()));
	}
}
